﻿using System.Text;

public class NetHandler_Dialogue : GameServerMessageHandler
{
    public const string Key = "Dialogue";

    public static NetHandler_Dialogue NewHandler()
    {
        return new NetHandler_Dialogue();
    }

    protected override ISerialize NewData() => new DialogueRequestData();

    public override void _Execute(TCPClient client, ISerialize _data)
    {
        DialogueRequestData requestData = _data as DialogueRequestData;

        GameDebug.Log($"用户{client.userID} {requestData?.fromUserId} 发送会话给:{requestData?.toUserId}");

        UserServer user = Game.instance.GetUser(client.userID);

        DialogueResponseData response = new DialogueResponseData();

        if (user == null)
        {
            GameDebug.LogError($"找不到用户：{client.userID}");
            response.code = ErrorCode.UserNotFound;
        }
        else
        {
            if (requestData != null)
            {
                bool validUserId = requestData.fromUserId != requestData.toUserId
                    && requestData.fromUserId > 0 && requestData.toUserId > 0;

                if (validUserId)
                {
                    Dialogue dialogue = requestData.dialogue;
                    dialogue.time = (long)CommonFunctions.GetTimeStampSeconds();
                    Chat chat = user.data.AddDialogue(requestData.fromUserId, requestData.toUserId, dialogue);

                    UserServer toUser = Game.instance.GetUser(requestData.toUserId);
                    if (toUser != null)
                    {
                        toUser.data.AddDialogue(requestData.fromUserId, requestData.fromUserId, dialogue);

                        GameDebug.Log($"发送会话事件给:{requestData.toUserId}");
                        NetEvent_Dialogue netEvent_Dialogue = new NetEvent_Dialogue();
                        netEvent_Dialogue.dialogue = new DialogueData();
                        netEvent_Dialogue.dialogue.dialogue = dialogue;

                        netEvent_Dialogue.SendToRemote(toUser.client);
                    }

                    string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(chat.dialogues);
                    string dialoguesStr = CommonFunctions.CompressJson(jsonStr);
                    GameDebug.Log($"data length :{dialoguesStr.Length} jsonStr length:{jsonStr.Length}");

                    response.dialogue = dialogue;

                    int ret = chats.SetChat(chat.userId1, chat.userId2, dialoguesStr/*, chat.newMsgCount*/);

                    if (ret >= 0)
                        response.code = 0;
                    else
                        response.code = -1;
                }
                else
                {
                    GameDebug.LogError($"invalid from:{requestData.fromUserId}  to:{requestData.toUserId}");
                    response.code = ErrorCode.InvalidUserId;
                }
            }
            else
            {
                GameDebug.LogError($"requestData == null _data:{_data != null} type:{_data.GetType()}");
                response.code = -1;
            }
        }

        byte[] data = response.Write();
        this.SendResponse(client, Key, this.m_MessageReceived.seq, data);
    }
}
